package com.jxgyl.common.util;

import java.util.Map;

/**
 * @description: 日志
 * @author: wanglipeng
 * @create: 2021-08-10 15:56
 **/

public class LogMessage {

    private final static String RESULT_PROCESSING = "processing";
    private final static String RESULT_SUCCESS = "success";
    private final static String RESULT_FAIL = "fail";
    private final static String ERROR_MESSAGE = "errorMessage";
    private final static String SEPARATOR = "|";

    private String module;
    private String method;
    private String result = RESULT_FAIL;

    private final static String JOIN_TIME = "joinTime";
    private final static String END_TIME = "endTime";

    private final static String PROCESS_TIME ="processTime";

    private Long joinTime;

    private StringBuilder bodyBuilder = new StringBuilder();

    public LogMessage(String module, String method) {
        this.joinTime = System.currentTimeMillis();

        this.module = module;
        this.method = method;
    }

    public String getModule() {
        return module;
    }

    public LogMessage setModule(String module) {
        this.module = module;
        return this;
    }

    public String getMethod() {
        return method;
    }

    public LogMessage setMethod(String method) {
        this.method = method;
        return this;
    }

    public String getResult() {
        return result;
    }

    public LogMessage setResult(String result) {
        this.result = result;
        return this;
    }

    public LogMessage append(String key, Object value) {
        bodyBuilder.append(SEPARATOR)
                .append("\"").append(key).append("\"").append("=")
                .append("\"").append(value).append("\"");
        return this;
    }

    public LogMessage setKeyValues(Map<String, Object> keyValues) {
        for (Map.Entry<String, Object> kv : keyValues.entrySet()) {
            append(kv.getKey(), kv.getValue());
        }
        return this;
    }

    public LogMessage processing() {
        result = RESULT_PROCESSING;
        return this;
    }

    public LogMessage success() {
        result = RESULT_SUCCESS;
        return this;
    }

    public LogMessage fail() {
        result = RESULT_FAIL;
        return this;
    }

    public LogMessage fail(String errorMessage) {
        append(ERROR_MESSAGE, errorMessage);
        return fail();
    }

    public LogMessage copy() {
        LogMessage logMessage = new LogMessage(module, method);
        logMessage.result = result;
        logMessage.bodyBuilder = new StringBuilder(bodyBuilder.toString());
        return logMessage;
    }

    @Override
    public String toString() {


        StringBuilder headerBuilder = new StringBuilder();
        headerBuilder.append(module).append(SEPARATOR).append(method);

        if (result != null) {
            headerBuilder.append(SEPARATOR).append(result);
        }
        headerBuilder.append(bodyBuilder.toString());
        Long endTime = System.currentTimeMillis();
        Long processTime = endTime - this.joinTime;
        //增加处理时间
        headerBuilder.append(SEPARATOR)
                .append("\"").append(JOIN_TIME).append("\"").append("=")
                .append("\"").append(joinTime).append("\"")
                .append(SEPARATOR)
                .append("\"").append(END_TIME).append("\"").append("=")
                .append("\"").append(endTime).append("\"")
                .append(SEPARATOR)
                .append("\"").append(PROCESS_TIME).append("\"").append("=")
                .append("\"").append(processTime).append("\"");

        return headerBuilder.toString();
    }
}
